
$(function(){

//获取所有的DOM节点
		// 依赖ngRoute模块
		var App = angular.module('App', ['ngRoute','NetworkRequest']);
// test()
        App.run()
		// 需要对路由模块进行配置，使其正常工作
		App.config(['$routeProvider', function ($routeProvider) {
			// 同一文件夹 可以访问 不同文件夹跨越请求
			$routeProvider.when('/article', {
				templateUrl: 'public/views/article/article.html',//视图模型
				controller: 'articleDetailContro', //定义控制器
			})
                .when('/',{
                    templateUrl: 'public/views/article/article.html',//视图模型
                    controller: 'articleDetailContro' //定义控制器
                })
			.when('/video', {
				templateUrl: 'public/views/video/video.html',
				controller:"videoDetailContro"
			})
			.when('/topic',{
				templateUrl: 'public/views/topic/topic.html',
				controller:"topicDetailContro"
			})
			.otherwise({
				redirectTo: 'public/views/article/article.html',
                controller: 'articleDetailContro' //定义控制器
			});

		}]);

		// 应用控制器
		App.controller("AppController",["$scope", function($scope){

			//默认发布文章
            $scope.publish = "发布文章"
            var article = $("#article a")
            var video = $("#video a ")
            var topic = $("#topic a")

            if(location.hash == "#/article"){
                $scope.publish = "发布文章"
                video.removeClass('active')
                topic.removeClass('active')
                article.addClass('active')
            }else if(location.hash == "#/video"){
                $scope.publish = "发布视频"
                article.removeClass('active')
                topic.removeClass('active')
                video.addClass('active')
            }else if(location.hash == "#/topic"){
                $scope.publish = "发布话题"
                article.removeClass('active')
                video.removeClass('active')
                topic.addClass('active')
            }

			$("#article").on("click",function(e){
				if( !article.hasClass('active')){
				    $scope.publish = "发布文章"
					video.removeClass('active')
					topic.removeClass('active')
					article.addClass('active')
				}

			})
			$("#video").on("click",function(){
				if( !video.hasClass('active')){
					$scope.publish = "发布视频"
					article.removeClass('active')
					topic.removeClass('active')
					video.addClass('active')
				}

			})
			$("#topic").on("click",function(){
				if( !topic.hasClass('active')){
					$scope.publish = "发布话题"
					article.removeClass('active')
					video.removeClass('active')
					topic.addClass('active')
				}

			})
			// 发布按钮点击 跳到发布页面
			$('.publish').on('click',function(){
				if($scope.publish == "发布文章"){
					window.location.href= $scope.router.toArticle
				}else if($scope.publish == "发布视频"){
					window.location.href= $scope.router.toVideo
				}else{
					window.location.href= $scope.router.toTopic

				}
			})
		}])

        //文章详情控制器
        App.controller("articleDetailContro", ['$scope', '$http',function($scope, $http){

           // 配置

            $scope.count = 0;
            $scope.p_pernum = 10;
            //变量
            $scope.p_current = 1;
            $scope.p_all_page =3;
            $scope.pages = [];
            //初始化第一页
            //          1           10
            _get($scope.p_current,$scope.p_pernum,function(){
                // alert("我是第一次加载");
            });

            //单选按钮选中
            $scope.select= function(id){
                // alert(id);
                if($scope.publish == "发布文章"){
                    window.location.href= $scope.router.toArticleDetail + id

                }
            }
            //首页
            $scope.p_index = function(){
                $scope.load_page(1);
            }
            //尾页
            $scope.p_last = function(){
                $scope.load_page($scope.p_all_page);
            }
            //加载某一页
            $scope.load_page = function(page){
                //      1   10
                _get(page,$scope.p_pernum,function(){ });
            };
            //获取数据
            function _get(page,count,callback){
                // public/views/article/article.php

                $scope.getArticleList(page , count, function(res){
                    if(res&&res.status==200){
                        console.log(res)
                        // 获取类型
                        // $scope.style = res.data[data.length-1]
                        $scope.count= res.data.total;//10 * 1 + 2
                        $scope.list=res.data.list;
                        $scope.p_current = page;
                        $scope.p_all_page =Math.ceil($scope.count/$scope.p_pernum);

                        reloadPno();
                        callback();
                    }else{
                        alert("加载失败,请先登录！");
                        window.location.href = $scope.router.tologin
                    }

                }, function (error) {
                    console.log(error)
                })

            }
            //初始化页码
            function reloadPno(){
                //                              1               2

                $scope.pages=calculateIndexes($scope.p_current,$scope.p_all_page,10);
                    console.log($scope.pages)
            };
           //分页算法
            function calculateIndexes(current, length, displayLength) {
                console.log(current, length, displayLength)
                var indexes = [];
                var start = Math.round(current - displayLength / 2);
                var end = Math.round(current + displayLength / 2);
                // console.log(start, end)

                if (start <= 1) {
                    start = 1;
                    end = start + displayLength - 1;
                    if (end >= length - 1) {
                        end = length - 1;
                    }
                    // console.log(start,end) // 1 10
                }
                if (end >= length - 1) {
                    end = length;
                    // start = end - displayLength + 1;
                    start = end - length + 1;
                    console.log(start,end)  // 2 11

                    if (start <= 1) {
                        start = 1;
                    }
                }
                console.log(start, end)
                for (var i = start; i <= end; i++) {
                    indexes.push(i);
                }
                return indexes;
            };
        }])
		//视频详情控制器
		App.controller("videoDetailContro", ['$scope', '$http',function($scope, $http){
          // 删除按钮
          $scope.src ="./public/img/delete.png"
            //鼠标移入
          $scope.deleteOver = function (img) {
              // console.log(img)
              img.src ="./public/img/delete_bgc.png"
          }
          //鼠标移出
          $scope.deleteOut = function (img) {
                // console.log(img)
                img.src ="./public/img/delete.png"
          }
          //删除点击
             $scope.deleteClick = function (img,videoId,videoTitle) {
                 console.log(videoId)
                 //删除视频请求
                 //确认删除按钮
                 var del =  window.confirm("您确定要删除" + videoTitle + "吗?");
                 if(del == true){
                     $scope.deleteVideo(
                         videoId,
                         function (res) {
                             if(res.status == 200 && res){
                                 alert("删除成功！")
                                 window.location.reload()
                             }else{
                                 alert("删除失败，请稍候重试！")
                             }
                         },
                         function (error) {
                             console.log(error)
                         }
                         )}else{
                     return
                 }
             }

            $scope.count = 0;
            $scope.p_pernum = 3;
            //变量
            $scope.p_current = 1;
            $scope.p_all_page =2;
            $scope.pages = [];
            //初始化第一页
            //          1           3
            _get($scope.p_current,$scope.p_pernum,function(){
                // alert("我是第一次加载");
            });


            //首页
            $scope.p_index = function(){
                $scope.load_page(1);
            }
            //尾页
            $scope.p_last = function(){
                $scope.load_page($scope.p_all_page);
            }
            //加载某一页
            $scope.load_page = function(page){
                //      1   10
                _get(page,$scope.p_pernum,function(){ });
            };
            //获取数据
            function _get(page,count,callback){

                $scope.getVideoList(page , count, function(res){
                    if(res&&res.status==200){
                        console.log(res)
                        // 获取类型
                        // $scope.style = res.data[data.length-1]
                        $scope.count= res.data.total;//10 * 1 + 2
                        $scope.list=res.data.list;
                        console.log($scope.list[0].imgURL)
                        $scope.p_current = page;
                        $scope.p_all_page =Math.ceil($scope.count/$scope.p_pernum);

                        reloadPno();
                        callback();
                    }else{
                        alert("加载失败,请重试！");
                    }

                }, function (error) {
                    console.log(error)
                })

            }
            //初始化页码
            function reloadPno(){
                //                                1               11
                $scope.pages=calculateIndexes($scope.p_current,$scope.p_all_page,10);
            };
            //分页算法
            function calculateIndexes(current, length, displayLength) {
                var indexes = [];
                var start = Math.round(current - displayLength / 2);
                var end = Math.round(current + displayLength / 2);
                if (start <= 1) {
                    start = 1;
                    end = start + length - 1;
                    if (end >= length - 1) {
                        end = length - 1;
                    }
                }
                if (end >= length - 1) {
                    end = length;
                    start = end - displayLength + 1;
                    if (start <= 1) {
                        start = 1;
                    }
                }
                for (var i = start; i <= end; i++) {
                    indexes.push(i);
                }
                return indexes;
            };

        }])


		//话题详情控制器
		App.controller("topicDetailContro", ['$scope', '$http',function($scope, $http){
            var article = $("#article a")
            var video = $("#video a ")
            var topic = $("#topic a")
            video.removeClass('active')
            article.removeClass('active')
            topic.addClass('active')



            $scope.count = 0;
            $scope.p_pernum = 10;
            //变量
            $scope.p_current = 1;
            $scope.p_all_page =3;
            $scope.pages = [];
            //初始化第一页
            //          1           10
            _get($scope.p_current,$scope.p_pernum,function(){
                // alert("我是第一次加载");
            });

            //单选按钮选中
            $scope.select= function(id){
                // alert(id);
                if($scope.publish == "发布话题"){
                    window.location.href=$scope.router.toTopicDetail+ id

                    // window.location.href="public/views/detail.html?type=topic&topicId=" + id
                }
            }
            //首页
            $scope.p_index = function(){
                $scope.load_page(1);
            }
            //尾页
            $scope.p_last = function(){
                $scope.load_page($scope.p_all_page);
            }
            //加载某一页
            $scope.load_page = function(page){
                //      1   10
                _get(page,$scope.p_pernum,function(){ });
            };
            console.log($scope.url.getTopicList)

            // $scope.getTopicList($scope.url.getTopicList, 1 , 10, 1, 2)


            //获取数据

            function _get(page,count,callback){

                $scope.getTopicList(page , count, function(res){
                    if(res&&res.status==200){
                        console.log(res)
                        // 获取类型
                        // $scope.style = res.data[data.length-1]
                        $scope.count= res.data.total;//10 * 1 + 2
                        $scope.list=res.data.list;
                        $scope.p_current = page;
                        $scope.p_all_page =Math.ceil($scope.count/$scope.p_pernum);

                        reloadPno();
                        callback();

                    }else{
                        alert("加载失败,请先登录！");
                    }

                }, function (error) {
                    console.log(error)
                })


            }
            //初始化页码
            function reloadPno(){
                //                                1               2
                $scope.pages=calculateIndexes($scope.p_current,$scope.p_all_page,10);
            };
            //分页算法
            function calculateIndexes(current, length, displayLength) {
                var indexes = [];
                var start = Math.round(current - displayLength / 2);
                var end = Math.round(current + displayLength / 2);
                if (start <= 1) {
                    start = 1;
                    end = start + displayLength - 1;
                    if (end >= length - 1) {
                        end = length - 1;
                    }
                }
                if (end >= length - 1) {
                    end = length;
                    start = end - length + 1;
                    if (start <= 1) {
                        start = 1;
                    }
                }
                for (var i = start; i <= end; i++) {
                    indexes.push(i);
                }
                return indexes;
            };

		}])


})
